<?php
/**
 * 预存款管理
 */

defined('ByShopWWI') or exit('Access Invalid!');

class predepositControl extends mobileMemberControl {
    public function __construct(){
        parent::__construct();

        if(empty($_SESSION['member_id'])){
            Tpl::showpage('login');
        }
    }

    /**
     * 我的钱包
     */
    public function moneybagOp(){
        Tpl::output('available_predeposit',$this->member_info['available_predeposit']);
        Tpl::output('freeze_predeposit',$this->member_info['freeze_predeposit']);
        Tpl::output('member_avatar',$this->member_info['member_avatar']);
        Tpl::showpage('moneybag');
    }

    /**
     * 账单记录明细
     */
    public function trade_logOp(){
        $model_pd = Model('predeposit');
        $condition = array();
        $condition['lg_member_id'] = $_SESSION['member_id'];

        $list = $model_pd->getPdLogList($condition,10,'*','lg_id desc');

        Tpl::output('list',$list);
        Tpl::output('totalPage',$model_pd->gettotalpage());
        Tpl::showpage('trade_log');
    }

    /**
     * 账单记录明细Ajax实现分页
     */
    public function trade_logListPageOp(){
        $_GET['curpage'] = $_GET['curpage']+1;//当前页

        $condition = array();
        $condition['lg_member_id'] = $_SESSION['member_id'];

        $model_pd = Model('predeposit');
        $list = $model_pd->getPdLogList($condition,10,'*','lg_id desc');

        foreach($list as $k => $v){
            $v['lg_add_time'] =  date('Y-m-d',$v['lg_add_time']);
            $v['lg_desc'] =  substr($v['lg_desc'],0,strrpos($v['lg_desc'],'，'));
            $v['lg_av_amount_desc'] = floatval($v['lg_av_amount']) ? (floatval($v['lg_av_amount']) > 0 ? '收入' : '支出' ) : null;
            $v['lg_av_amount_show'] = floatval($v['lg_av_amount']) ? (floatval($v['lg_av_amount']) > 0 ? '+' : null ).$v['lg_av_amount'] : null;
            $arr[] = $v;
        }
        $list = $arr;

        output_data(array('list_log' => $list,'curpage' => $_GET['curpage']));
    }

    /**
     * init充值页面
     */
    public function rechargeOp(){
        Tpl::showpage('recharge');
    }

    /**
     * 充值列表
     */
    public function recharge_logOp(){
        $condition = array();
        $condition['pdr_member_id'] = $_SESSION['member_id'];
        $condition['pdr_payment_state'] = 1;//查看已充值记录

        $model_pd = Model('predeposit');
        $list = $model_pd->getPdRechargeList($condition,'','*','pdr_id desc');

        Tpl::output('list',$list);
        Tpl::showpage('recharge_log');
    }

    //init提现页面
    public function cash_outOp(){


        $buyer_info = Model('member')->getMemberInfoByID($_SESSION['member_id'],'member_paypwd');

        if(empty($buyer_info['member_paypwd'])){
            //未设置交易密码
            Tpl::output('title','未设置支付密码');
            Tpl::output('info','请设置完成支付密码再进行此操作，3秒后跳转至支付密码设置页。</br><a style="color:#f0b33e;" href="/app/index.php?act=member&op=paymentpassword" target="_self">设置支付密码</a>');
            Tpl::output('url','/app/index.php?act=member&op=paymentpassword');
            Tpl::output('time','3000');
            Tpl::showpage('msg_show');

        }else{
            Tpl::output('available_predeposit',$this->member_info['available_predeposit']);
            Tpl::showpage('cash_out');
        }
    }

    /**
     * 提现列表
     */
    public function cash_outlogOp(){
        $condition = array();
        $condition['pdc_member_id'] =  $_SESSION['member_id'];

        $model_pd = Model('predeposit');
        $cash_list = $model_pd->getPdCashList($condition,'','*','pdc_id desc');

        Tpl::output('list',$cash_list);
        Tpl::showpage('cash_outlog');
    }

    /**
     * 申请提现
     */
    public function pd_cash_addOp(){
        $pdc_amount = abs(floatval($_POST['pdc_amount']));
        $model_pd = Model('predeposit');
        $model_member = Model('member');
        $member_info = $model_member->getMemberInfoByID($_SESSION['member_id']);

        try {
            $model_pd->beginTransaction();
            $pdc_sn = $model_pd->makeSn();
            $data = array();
            $data['pdc_sn'] = $pdc_sn;
            $data['pdc_member_id'] = $_SESSION['member_id'];
            $data['pdc_member_name'] = $_SESSION['member_name'];
            $data['pdc_amount'] = $pdc_amount;
            $data['pdc_bank_name'] = $_POST['pdc_bank_name'];
            $data['pdc_bank_no'] = $_POST['pdc_bank_no'];
            $data['pdc_bank_user'] = $_POST['pdc_bank_user'];
            $data['pdc_add_time'] = TIMESTAMP;
            $data['pdc_payment_state'] = 0;
            $insert = $model_pd->addPdCash($data);
            if (!$insert) {
                // throw new Exception(Language::get('predeposit_cash_add_fail'));
                Tpl::showpage('order_pay_fail');
                return;
            }
            //冻结可用预存款
            $data = array();
            $data['member_id'] = $member_info['member_id'];
            $data['member_name'] = $member_info['member_name'];
            $data['amount'] = $pdc_amount;
            $data['order_sn'] = $pdc_sn;
            $model_pd->changePd('cash_apply',$data);
            $model_pd->commit();
            Tpl::showpage('cash_succee');
        } catch (Exception $e) {
            $model_pd->rollback();
            Tpl::showpage('order_pay_fail');
        }
    }



    /**
     * 充值添加
     */
    public function recharge_addOp(){
        if (!chksubmit()){
            //信息输出
            self::profile_menu('recharge_add','recharge_add');
            Tpl::showpage('predeposit.pd_add');
            exit();
        }
        $pdr_amount = abs(floatval($_POST['pdr_amount']));
        if ($pdr_amount <= 0) {
            showMessage(Language::get('predeposit_recharge_add_pricemin_error'),'','html','error');
        }
        $model_pdr = Model('predeposit');
        $data = array();
        $data['pdr_sn'] = $pay_sn = $model_pdr->makeSn();
        $data['pdr_member_id'] = $_SESSION['member_id'];
        $data['pdr_member_name'] = $_SESSION['member_name'];
        $data['pdr_amount'] = $pdr_amount;
        $data['pdr_add_time'] = TIMESTAMP;
        $insert = $model_pdr->addPdRecharge($data);
        if ($insert) {
            //转向到商城支付页面
            redirect(SHOP_SITE_URL . '/index.php?act=buy&op=pd_pay&pay_sn='.$pay_sn);
        }
    }

    /**
     * 平台充值卡
     */
    public function rechargecard_addOp()
    {
        if (!chksubmit()) {
            self::profile_menu('rechargecard_add','rechargecard_add');
            Tpl::showpage('predeposit.rechargecard_add');
            return;
        }

        $sn = (string) $_POST['rc_sn'];
        if (!$sn || strlen($sn) > 50) {
            showMessage('平台充值卡卡号不能为空且长度不能大于50', '', 'html', 'error');
            exit;
        }

        try {
            model('predeposit')->addRechargeCard($sn, $_SESSION);
            showMessage('平台充值卡使用成功', urlMember('predeposit', 'rcb_log_list'));
        } catch (Exception $e) {
            showMessage($e->getMessage(), '', 'html', 'error');
            exit;
        }
    }



    /**
     * 查看充值详细
     *
     */
    public function recharge_showOp(){
        $pdr_id = intval($_GET["id"]);
        if ($pdr_id <= 0){
            showDialog(Language::get('predeposit_parameter_error'),'','error');
        }

        $model_pd = Model('predeposit');
        $condition = array();
        $condition['pdr_member_id'] = $_SESSION['member_id'];
        $condition['pdr_id'] = $pdr_id;
        $condition['pdr_payment_state'] = 1;
        $info = $model_pd->getPdRechargeInfo($condition);
        if (!$info){
            showDialog(Language::get('predeposit_record_error'),'','error');
        }
        Tpl::output('info',$info);
        self::profile_menu('rechargeinfo','rechargeinfo');
        Tpl::showpage('predeposit.pd_info');
    }

    /**
     * 删除充值记录
     *
     */
    public function recharge_delOp(){
        $pdr_id = intval($_GET["id"]);
        if ($pdr_id <= 0){
            showDialog(Language::get('predeposit_parameter_error'),'','error');
        }

        $model_pd = Model('predeposit');
        $condition = array();
        $condition['pdr_member_id'] = $_SESSION['member_id'];
        $condition['pdr_id'] = $pdr_id;
        $condition['pdr_payment_state'] = 0;
        $result = $model_pd->delPdRecharge($condition);
        if ($result){
            showDialog(Language::get('nc_common_del_succ'),'reload','succ','CUR_DIALOG.close()');
        }else {
            showDialog(Language::get('nc_common_del_fail'),'','error');
        }
    }



    /**
     * 充值卡余额变更日志
     */
    public function rcb_log_listOp()
    {
        $model = Model();
        $list = $model->table('rcb_log')->where(array(
            'member_id' => $_SESSION['member_id'],
        ))->page(20)->order('id desc')->select();

        //信息输出
        self::profile_menu('log', 'rcb_log_list');
        Tpl::output('show_page', $model->showpage());
        Tpl::output('list', $list);
        Tpl::showpage('predeposit.rcb_log_list');
    }





    /**
     * 提现记录详细
     */
    public function pd_cash_infoOp(){
        $pdc_id = intval($_GET["id"]);
        if ($pdc_id <= 0){
            showMessage(Language::get('predeposit_parameter_error'),'index.php?act=predeposit&op=pd_cash_list','html','error');
        }
        $model_pd = Model('predeposit');
        $condition = array();
        $condition['pdc_member_id'] = $_SESSION['member_id'];
        $condition['pdc_id'] = $pdc_id;
        $info = $model_pd->getPdCashInfo($condition);
        if (empty($info)){
            showMessage(Language::get('predeposit_record_error'),'index.php?act=predeposit&op=pd_cash_list','html','error');
        }

        self::profile_menu('cashinfo','cashinfo');
        Tpl::output('info',$info);
        Tpl::showpage('predeposit.pd_cash_info');
    }

    /**
     * 用户中心右边，小导航
     *
     * @param string    $menu_type  导航类型
     * @param string    $menu_key   当前导航的menu_key
     * @return
     */
    private function profile_menu($menu_type,$menu_key=''){
        $menu_array = array(
            array('menu_key'=>'loglist',        'menu_name'=>'账户余额',    'menu_url'=>'index.php?act=predeposit&op=pd_log_list'),
            array('menu_key'=>'recharge_list',  'menu_name'=>'充值明细',    'menu_url'=>'index.php?act=predeposit&op=index'),
            array('menu_key'=>'cashlist',       'menu_name'=>'余额提现',    'menu_url'=>'index.php?act=predeposit&op=pd_cash_list'),
            // array('menu_key'=>'rcb_log_list',   'menu_name'=>'充值卡余额',   'menu_url'=>'index.php?act=predeposit&op=rcb_log_list',),
        );
        switch ($menu_type) {
            case 'rechargeinfo':
                $menu_array[] = array('menu_key'=>'rechargeinfo','menu_name'=>'充值详细',  'menu_url'=>'');
                break;
            case 'recharge_add':
                $menu_array[] = array('menu_key'=>'recharge_add','menu_name'=>'在线充值',   'menu_url'=>'');
                break;
            case 'rechargecard_add':
                $menu_array[] = array('menu_key'=>'rechargecard_add','menu_name'=>'充值卡充值','menu_url'=>'javascript:;');
                break;
            case 'cashadd':
                $menu_array[] = array('menu_key'=>'cashadd','menu_name'=>'提现申请',    'menu_url'=>'index.php?act=predeposit&op=pd_cash_add');
                break;
            case 'cashinfo':
                $menu_array[] = array('menu_key'=>'cashinfo','menu_name'=>'提现详细',  'menu_url'=>'');
                break;
            case 'log':
            default:
                break;
        }
        Tpl::output('member_menu',$menu_array);
        Tpl::output('menu_key',$menu_key);
    }
}
